//
//  ViewController.m
//  jsonWeatherDemo
//
//  Created by yorkfeng on 15/12/14.
//  Copyright © 2015年 yorkfeng. All rights reserved.
//

/*
 从IOS5开始，APPLE提供了对json的原生支持（NSJSONSerialization）
 
 iOS9.0SDK在编译的时候，默认所有从NSURLConnection、CFURL和NSURLSession发出的http请求，都改为https请求。由于AFNetworking版本底层是用了NSURLConnection，所以使用AFNetworking的app都将受到影响。对于这个问题的解决办法有三种，一：让服务器更新，使用https，以解析相关的数据。二：在info.plist做些配置，让其暂时倒回到不安全的网络传输协议(http协议)。下面详细来说下这两种方案。三：设置域，也就是把不支持https协议的接口设置成http的接口。
 
 方案二：暂时倒回到不安全的网络传输协议(http协议)
 
 （1）、在info.plist中增加一个AppTransportSecuritySetting字段，类型是字典。
 
 （2）、然后给其增加一个Key：AllowsArbitraryLoads，类型是Boolean类型，值为YES。
 
 参考网址：http://my.oschina.net/daguoshi/blog/508547
 */

#import "ViewController.h"

@interface ViewController () {
    
    NSDictionary *weatherDic;

    __weak IBOutlet UITextView *text;
    __weak IBOutlet UITextView *text2;
}

@property (weak, nonatomic) IBOutlet UIButton *btn;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btnDown:(id)sender {
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0) {
        //ios9.0
        NSURLSession *session = [NSURLSession sharedSession];
        [[session dataTaskWithURL:[NSURL URLWithString:@"http://www.weather.com.cn/data/cityinfo/101210301.html"]
                completionHandler:^(NSData *data,
                                    NSURLResponse *response,
                                    NSError *error) {
                    // handle response
                
                    if (data == nil) {
                        NSLog(@"data == nil!!");
                        return;
                    }
                
                    //    iOS5自带解析类NSJSONSerialization从data中解析出数据放到字典中
                    weatherDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
                    //将NSData转化为json
                    //+ (nullable NSData *)dataWithJSONObject:(id)obj options:(NSJSONWritingOptions)opt error:(NSError **)error;
                
                }] resume];
    }
}

- (IBAction)btnClick:(id)sender {
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue] <= 9.0) {
        //sendSynchronousRequest: returningResponse: error: 方法自9.0后被NSURLSession替代
     
        NSError *error;
    
        //    加载一个NSURL对象
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.weather.com.cn/data/cityinfo/101210301.html"]];
    
        //    将请求的url数据放到NSData对象中
        NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    
        //    iOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中
        weatherDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
     
     }
    

    
    //    weatherDic字典中存放的数据也是字典型，从它里面通过键值取值
    NSDictionary *weatherInfo = [weatherDic objectForKey:@"weatherinfo"];
    
    //    打印出weatherInfo字典所存储数据
    NSLog(@"%@\n今天的天气状况是：\n最高温度：%@  最低温度：%@\n%@ ",[weatherInfo objectForKey:@"city"], [weatherInfo objectForKey:@"temp1"], [weatherInfo objectForKey:@"temp2"], [weatherInfo objectForKey:@"weather"]);
    
    [text setText:[NSString stringWithFormat:@"%@\n今天的天气状况是：\n最高温度：%@  最低温度：%@\n%@ ",[weatherInfo objectForKey:@"city"], [weatherInfo objectForKey:@"temp1"], [weatherInfo objectForKey:@"temp2"], [weatherInfo objectForKey:@"weather"]]];
    
    NSLog(@"weatherInfo字典里面的内容是--->%@",[weatherInfo description]);
    [text2 setText:[NSString stringWithFormat:@"weatherInfo字典里面的内容是--->%@",[weatherInfo description]]];
}

@end
